<template>
  <div class="articleList" ref="artList">
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell v-for="item in list " :key="item.art_id" :title="item.title"  @click="$router.push({
          path:'/article',query:{id:item.art_id}})">
          <!--  -->
         <template #label>
            <van-grid :border="false" :column-num="item.cover.type" >
            <!-- 新闻内容 -->
            <van-grid-item  v-for="(value,index) in item.cover.images" :key="index">
              <van-image :src="value" />
            </van-grid-item>
          </van-grid>
<!-- 作者 -->
           <div>
           <span>{{item.aut_name}}</span>
           <span style="margin: 0 10px">评论：{{item.comm_count}}</span>
           <span>{{item.pubdate | relvTime}}</span>
           
            <van-icon name="cross" style="float: right" />
         </div>
         </template>
        
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { articleListAPI } from "@/api";
export default {
  props: {
    channel_id: {
      // 必须传
      required: true,
    },
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      isLoading: false,
      timestamp: Date.now(),
        // 保存滚动条距离
      scrollTop: 0,
    };
  },
  methods: {
    async onLoad() {
      let res = await articleListAPI({
        channel_id: this.channel_id,
        timestamp: this.timestamp,
      });
      // console.log(res);
      // 将获取到的数据push到list
      this.list.push(...res.data.data.results)
      this.timestamp=res.data.data.pre_timestamp
      //   // 将 loading 设置为 true，表示处于加载状态
        this.loading = false;

        if(this.timestamp == null){
           this.finished = true;
        }

    },
  async  onRefresh() {
       
      let res = await articleListAPI({
        channel_id:this.channel_id,
      //   // 获取到最新时间
        timestamp:Date.now()
      });

//   // 把请求到的最新数据直接覆盖
this.list = res.data.data.results
      // 把下一段的时间戳赋值一下，方便用户做上拉列表刷新
      this.timestamp = res.data.data.pre_timestamp
            // 把下拉刷新状态改为false
            this.isLoading = false
  
    },
    
  },
  mounted(){
     this.$refs.artList.onscroll=()=>{
      //  内容向上滚得距离 =dom盒子滚得距离
       this.scrollTop = this.$refs.artList.scrollTop;
     }
    },
     activated() {
  //   // 激活时再把之前的滚动条的高度赋值给滚动条，让它滚到对应的位置
    this.$refs.artList.scrollTop = this.scrollTop;
  },
};
</script>

<style lang = "less" scoped>
.articleList {
  position: fixed;
  left: 0;
  top: 90px;
  /* // 是nav-bar高度是46，tabs是44，加起来90 */
  bottom: 50px;
  /* // 这是tabbar的高度 */
  overflow: scroll;
  width: 100%;
}
</style>